// 获取当前日期
function getDateTime(t) {
	var d = new Date(t);
	return Ext.util.Format.date(d, 'Y-m-d H:i:s');
}

function getDate(t) {
	if (t == undefined || t == "")
		return "";
	var d = new Date(t);
	return Ext.util.Format.date(d, 'Y-m-d');
}
function getCurrentDate() {
	var d = new Date();
	return Ext.util.Format.date(d, 'Y-m-d');
}
// 模态弹出框
function mkWindow(title, items, callbackFunc) {
	if (!items)
		return;
	return Ext.create('Ext.window.Window', {
		title : title,
		constrain : true,
		constrainHeader : true,
		bodyStyle : {
			overflow : true
		},
		modal : true,
		items : items,
		listeners : {
			afterrender : function(me, opts) {
				var w = 0;
				me.items.each(function(item, index, length) {
					var tw = item.getWidth();
					w = w > tw ? w : tw;
				});
				me.setWidth(w + 20);
				me.center();
				me.addListener('resize', function(me, w, h, eOpts) {
					me.items.getAt(0).setSize(w - 20, h - 50);
				});
			},
			beforeclose : function(me) {
				if (undefined == callbackFunc) {
					return true;
				} else
					callbackFunc();
			}
		}
	}).show();
}
// 模态弹出框,含loading提示信息
function mkWindows(title, items, callbackFunc) {
	if (!items)
		return;
	return Ext.create('Ext.window.Window', {
		title : title,
		constrain : true,
		constrainHeader : true,
		bodyStyle : {
			overflow : true
		},
		modal : true,
		items : items,
		listeners : {
			afterrender : function(me, opts) {
				var w = 0;
				me.items.each(function(item, index, length) {
					var tw = item.getWidth();
					w = w > tw ? w : tw;
				});
				me.setWidth(w + 20);
				me.center();
				me.addListener('resize', function(me, w, h, eOpts) {
					me.items.getAt(0).setSize(w - 20, h - 50);
				});
				me.body.mask('数据加载中...');
			},
			beforeclose : function(me) {
				if (undefined == callbackFunc) {
					return true;
				} else
					callbackFunc();
			}
		}
	}).show();
}


//初始化函数
function initFrame() {
	var Panel_header = Ext.create('Ext.panel.Panel', {
		region : 'north',
		height:103,
		html:'<img src="workplace/image/topnavigation02.jpg" />'
	});
	var centerPanel = Ext.create("Ext.tab.Panel", {
		region : 'center',
		id : 'mainCont',
		layout : 'border',
		bodyStyle : {
			background : 'transparent'
		}
		});
	Ext.create('Ext.container.Viewport', {
		renderTo : Ext.getBody(),
		layout : 'border',
		collapsed : false,
		items : [Panel_header, mkFuncMenuTree(), centerPanel]
	});
	//我的工作台
	if (null != waitLoadMask)
		waitLoadMask.hide();
		waitLoadMask = new Ext.LoadMask(Ext.getCmp('mainCont'), {
		msg : "加载中，请稍后..."
	});
	Ext.Ajax.request({
		url : 'user_detail.action',
		success : function(resp) {
			centerPanel.add(eval(resp.responseText));
			centerPanel.setActiveTab(0);
			waitLoadMask.hide();
		}
	});
}

// 左侧主界面菜单
function mkFuncMenuTree() {
	Ext.define('funcMenuModel', {
		extend : 'Ext.data.Model',
		fields : ['text', 'url', 'tag', 'icon'],// text 必须的 否则得设置,
		proxy : {
			type : 'ajax',
			url : 'json_demo/funcMenu.json'
		}
	});
	var funcTreeStore = Ext.create('Ext.data.TreeStore', {
		model : 'funcMenuModel',
		root : {
			text : '',
			expanded : true
		}
	});

	return Ext.create("Ext.tree.Panel", {
		region : 'west',
		collapsible : true,
		split : true,
		title : '功能菜单',
		width : 150,
		rootVisible : false,
		store : funcTreeStore,
		resizable : true,
		useArrows : true,
		tools : [{
		type : 'refresh',
		handler : function() {
			var tree = this.up('treepanel');
			tree.getStore().load();
		}
		}],
		listeners : {
			itemclick : function(me, rec, item, index, e, eOpts) {
				mkTabMain('mainCont', rec.get('text'), rec.get('url'));
			}
		}
	});
}

var waitLoadMask = null;
// tab
// 主界面id 为mainCont
function mkTabMain(id, title, url) {
	if ("#" === url)
			return;
	id = ('' === id ? 'mainCont' : id);
	var modelPanel = Ext.getCmp(id);
	var text = title;
	var f = false;
	if (null != waitLoadMask)
		waitLoadMask.hide();
	waitLoadMask = new Ext.LoadMask(Ext.getCmp(id), {
		msg : "加载中，请稍后..."
	});
	waitLoadMask.show();
	modelPanel.items.each(function(name, index, countriesItSelf) {
		if (this.title && (this.title.indexOf(text) > -1)) {
			this.up().setActiveTab(this.id);
			f = true;
			waitLoadMask.hide();
			return;
		}
	});
	if (!f) {
		Ext.Ajax.request({
			type: 'ajax',
	        url : 'islogin.action',
	        method : 'POST',
	        success : function(response, opts) {
				 var retData = response.responseText;
				 if(retData.indexOf("true")>-1){
				 	true ? Ext.Ajax.request({
						url : url,
						success : function(resp) {
							
							modelPanel.add(eval(resp.responseText));
							modelPanel
									.setActiveTab(modelPanel.items.getCount() - 1);
									
							
							waitLoadMask.hide();
						}
					}) : modelPanel.add(Ext.create('Ext.panel.Panel', {
						title : title,
						html : ' ' + title + '<br/><br/>',
						closable : true,
						frame : true,
						bodyPadding : 0,
						loader : {
							url : url,
							loadMask : true,
							failture : function(me, resp, opts) {
								Ext.Msg.alert('Load jsp failture !');
							},
							scripts : true,
							autoLoad : true
						}
					}));
					modelPanel.setActiveTab(modelPanel.items.getCount() - 1);
				}else{
					waitLoadMask.hide();
				 	Ext.Msg.alert('提示', '登录失效', function(){
					    location.href = "login.jsp";
					});
				 }
			},
			failure : function(response, opts) {
				waitLoadMask.hide();
				Ext.Msg.alert("提示","加截失败...");
			}
		});
	}
	
}

//处理请求数据的success以及failure公用方法
function funSuccess(response,opts,win,grid)
{
	/*if(response.indexOf("not privlege")>-1){
		Ext.Msg.alert('提示','没有权限操作此功能');
	}else{ */
		Ext.Msg.alert('提示','操作成功');
		win.close();
    	grid.getStore().load();
	//}
}
function funFailure(response,opts)
{
		Ext.Msg.alert('提示','操作失败');
}
/*下拉框return的值,fromStore:来源，value查找的值,searchfield:查找的Model字段名称,returnField:返回的Model字段名称*/
function getTextByStore(fromStore,value,searchfield,returnField){
	fromStore.each(function(obj){
		if(obj.get(searchfield)==value)
		{
			value= obj.get(returnField);
		}
	});
	return value;
}

























